package com.vitabuybuy.tv.ui.mine.order.after.viewmodel

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.LoadingDialogEntity
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.NetUrl
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.AfterSaleDetailDto
import com.vitabuybuy.tv.dto.AfterSaleListDto
import com.vitabuybuy.tv.dto.FirstCategoryDto
import com.vitabuybuy.tv.dto.NegotiationDto
import com.vitabuybuy.tv.network.AfterSaleRepository
import com.vitabuybuy.tv.network.AuthRepository
import java.util.*

class AfterSaleViewModel : BaseViewModel() {


    val orderlist = MutableLiveData<MutableList<AfterSaleListDto>>()

    fun getOrderList(pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                val it = AfterSaleRepository.getAfterSaleOrderList(pageNumber).await()
                orderlist.value = it
//                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    val detail = MutableLiveData<AfterSaleDetailDto>()

    fun getAfterSaleDetail(id: Int?) {
        rxHttpRequest {
            onRequest = {
                detail.value = AfterSaleRepository.getAfterSaleDetail(id).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun withdrawTheApplication(id: Int?) {
        rxHttpRequest {
            onRequest = {
                AfterSaleRepository.withdrawTheApplication(id).await()
                defUI.msgEvent.postValue(MessageModel(2))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun uploadImage(paths: List<String>) {
        defUI.loading.value = LoadingDialogEntity(isShow = true)
        val images = ArrayList<String>()
        for (file in paths) {
            rxHttpRequest {
                onRequest = {
                    val it = AuthRepository.upLoadImage(file).await()
                    images.add(NetUrl.FILE_PROFILE + it)
                    if (images.size == paths.size) {
                        defUI.loading.value = LoadingDialogEntity(isShow = false)
                        defUI.msgEvent.postValue(
                            MessageModel(1, obj = images)
                        )
                    }
                }
                onError = {
                    defUI.loading.value = LoadingDialogEntity(isShow = true)
                    XToast.normal(it.msg)
                }
            }
        }
    }

    fun applyReson(params: HashMap<String, Any>) {
        rxHttpRequest {
            onRequest = {
                AfterSaleRepository.applyAfterSale(params).await()
                defUI.msgEvent.postValue(MessageModel(2))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun haveSend(
        id: Int?,
        logisticsName: String,
        logisticsNumber: String,
        returnDescription: String
    ) {
        rxHttpRequest {
            onRequest = {
                AfterSaleRepository.haveSend(id, logisticsName, logisticsNumber, returnDescription)
                    .await()
                defUI.msgEvent.postValue(MessageModel(3))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    val firstCategorylist = MutableLiveData<MutableList<FirstCategoryDto>>()
    fun getLogisticsCompany() {
        rxHttpRequest {
            onRequest = {
                firstCategorylist.value = AfterSaleRepository.getLogisticsCompany().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun modifyAfterSale(params: HashMap<String, Any>) {
        rxHttpRequest {
            onRequest = {
                AfterSaleRepository.modifyAfterSale(params) .await()
                defUI.msgEvent.postValue(MessageModel(2))

            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    val negotiationList = MutableLiveData<MutableList<NegotiationDto>>()

    fun getNegotiationList(id: Int?) {
        rxHttpRequest {
            onRequest = {
                val it = AfterSaleRepository.getNegotiationList(id).await()
                negotiationList.value = it
                statusView.set(LoadingType.SHOW_CONTENT)
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = true
        }
    }
}
